<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>执行一个正则表达式搜索和替换</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pcre.html">PCRE 函数</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-grep.html">preg_grep</a></div>
 <div class="up"><a href="ref.pcre.html">PCRE 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.preg-filter" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_filter</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">preg_filter</span> &mdash; <span class="dc-title">执行一个正则表达式搜索和替换</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-filter-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>preg_filter</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pattern</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$subject</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$count</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>preg_filter()</strong></span>等价于<span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>
   除了它仅仅返回(可能经过转化)与目标匹配的结果. 这个函数怎样工作的更详细信息请阅读
   <span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>文档.
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-filter-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  如果<em><code class="parameter">subject</code></em>是一个<span class="type"><span class="type 数组">数组</span></span>，返回一个<span class="type"><span class="type 数组">数组</span></span>，
  其他情况返回一个<span class="type"><span class="type 字符串">字符串</span></span>。
  </p>
  <p class="para">
  如果没有找到匹配或者发生了错误，当<em><code class="parameter">subject</code></em>是<span class="type"><span class="type 数组">数组</span></span>
  时返回一个空<span class="type"><span class="type 数组">数组</span></span>，其他情况返回<strong><code>NULL</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-filter-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4684">
    <p><strong>Example #1 
     比较<span class="function"><strong>preg_filter()</strong></span> 
     和<span class="function"><a href="function.preg-replace.html" class="function">preg_replace()</a></span>的示例
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$subject&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'2'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'3'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'4'</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$pattern&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'/\d/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/[a-z]/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/[1a]/'</span><span style="color: #007700">);&nbsp;<br /></span><span style="color: #0000BB">$replace&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'A:$0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B:$0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'C:$0'</span><span style="color: #007700">);&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"preg_filter&nbsp;returns\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_filter</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$subject</span><span style="color: #007700">));&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"preg_replace&nbsp;returns\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$subject</span><span style="color: #007700">));&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
preg_filter returns
Array
(
    [0] =&gt; A:C:1
    [1] =&gt; B:C:a
    [2] =&gt; A:2
    [3] =&gt; B:b
    [4] =&gt; A:3
    [7] =&gt; A:4
)
preg_replace returns
Array
(
    [0] =&gt; A:C:1
    [1] =&gt; B:C:a
    [2] =&gt; A:2
    [3] =&gt; B:b
    [4] =&gt; A:3
    [5] =&gt; A
    [6] =&gt; B
    [7] =&gt; A:4
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-filter-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pcre.pattern.html" class="link">PCRE Patterns</a></li>
    <li class="member"><span class="function"><a href="function.preg-replace.html" class="function" rel="rdfs-seeAlso">preg_replace()</a> - 执行一个正则表达式的搜索和替换</span></li>
    <li class="member"><span class="function"><a href="function.preg-replace-callback.html" class="function" rel="rdfs-seeAlso">preg_replace_callback()</a> - 执行一个正则表达式搜索并且使用一个回调进行替换</span></li>
    <li class="member"><span class="function"><a href="function.preg-grep.html" class="function" rel="rdfs-seeAlso">preg_grep()</a> - 返回匹配模式的数组条目</span></li>
    <li class="member"><span class="function"><a href="function.preg-last-error.html" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - 返回最后一个PCRE正则执行产生的错误代码</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pcre.html">PCRE 函数</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.preg-grep.html">preg_grep</a></div>
 <div class="up"><a href="ref.pcre.html">PCRE 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
